import java.io.*;
import java.net.Socket;

class ServerPerformer extends Thread implements userData {
    private static final Integer OVERDRAFT = 1000;
    private static final String USERPIN = "1234";
    private static volatile Integer TOTAL = 10500;
    public static final String NO_FUNDS = "INSUFFICIENT FUNDS";
    public static final String BALANCE = "THE BALANCE ON YOUR CARD IS   ";
    private static final String WRONG_PIN = "ENTERED WRONG PIN NUMBER";
    private static final String GET = "GET";
    Socket socket;
    String message = null;
    String check_the_pin = null;
    String toGetCashOrder = null;

    ServerPerformer(Socket soc) {
        socket = soc;
        start();
    }

    public void run() {
        try {
            InputStream inputS = socket.getInputStream();
            DataInputStream inputData = new DataInputStream(inputS);
            message = inputData.readUTF();
            System.out.println("received " + message);
            if (checkPin(USERPIN)) {
                return;
            }

            actionRequired();

            OutputStream outputS = socket.getOutputStream();
            DataOutputStream outputData = new DataOutputStream(outputS);
            outputData.writeUTF(BALANCE + TOTAL.toString());
            System.out.println("sent " + BALANCE + TOTAL.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
    }

    public void closeSocket() {
        try {
            socket.close();
        } catch (IOException e) {
            System.err.println("Socket was not closed");
        }
    }

    public void actionRequired() {
        toGetCashOrder = message.substring(20, 23);
        if (toGetCashOrder.equals(GET)) {
            getMoney();
        }
    }

    public void getMoney() {
        System.out.println(message);
        String wantCash = message.substring(29, 33);
        System.out.println(wantCash);
        int value = Integer.parseInt(wantCash);
        System.out.println(value);

        if (value <= TOTAL + OVERDRAFT) {
            TOTAL = TOTAL - value;
            doAtmGiveMoney(value);
            sentSmsNotification(value);
        } else {
            System.out.println(NO_FUNDS);
            sentSmsNotification();
        }
    }

    public boolean sentSmsNotification() {
        System.out.println("attempt to withdraw money");
        return true;
    }

    private boolean sentSmsNotification(int value) {
        System.out.println("sms about withdrawn from account was sent. Withdraw " + value);
        return true;
    }

    public boolean doAtmGiveMoney(int value) {
        // Request for the hardware method should be added here
        System.out.println("money to withdraw , " + value);
        return true;
    }

    public boolean checkPin(String userPin) throws IOException {
        check_the_pin = message.substring(12, 16);
        while (!check_the_pin.equals(userPin)) {
            System.out.println(WRONG_PIN);
            OutputStream outputS = socket.getOutputStream();
            DataOutputStream outputData = new DataOutputStream(outputS);
            outputData.writeUTF(WRONG_PIN);
            closeSocket();
            return true;
        }
        return false;
    }
}
